// 借款人管理
app.controller('borrower', ['$scope', 'commonFactory', 'toaster','$timeout', function ($scope, commonFactory, toaster,$timeout) {
    $scope.BorrData = {
        Index: 1,
        Size: 10,
        Id:'',
        FullName:'',
        Mobile: ''
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.BorrData.Index >= $scope.page) {//判断最大页码
                $scope.BorrData.Index = $scope.page;
                return false;
            }
            $scope.BorrData.Index++
            requestBorrList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.BorrData.Index <= 1) {//判断最小页码
            $scope.BorrData.Index = 1;
            return false;
        }
        $scope.BorrData.Index--;
        requestBorrList()
    }
    // 查询
    $scope.queryBorr = function () {
        $scope.graybtn = true;
        $scope.BorrData.Index = 1;
        requestBorrList();
    }
     $scope.isadd=true;
     $scope.addBorrMoblie = ""
    $scope.addBorr = function () {
        if (checkinp()&&$scope.isadd) {
            $scope.isadd=false
            commonFactory.loading(true)
            commonFactory.ajax("/api/Borrower/InsertBorrower", {mobile:$scope.addBorrMoblie} , function (result) {
                commonFactory.loading(false)
                $scope.isadd=true;
                if (result.Code == 1) {
                    toaster.pop('success', '', '添加借款人成功');
                } else {
                    toaster.pop('warning', '', '添加借款人失败');
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '', '后台错误');
            }, "post");
        }
    }
    var checkinp = function () {
        if (!$scope.addBorrMoblie) {
            toaster.pop('warning', '', '请输入借款人手机号');
            return
        }
        return true
    }
    var requestBorrList = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Borrower/GetBorrowerList", $scope.BorrData, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if (result.Count >= 0) {
                $scope.BorrList = result.Data;
                $scope.BorrListCount = result.Count;
                $scope.page = Math.ceil(result.Count / $scope.BorrData.Size);
                // for (var i = $scope.BorrList.length - 1; i >= 0; i--) {
                //     $scope.BorrList[i].BirthDate = new Date().getFullYear()-new Date($scope.BorrList[i].BirthDate).getFullYear();
                // }
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requestBorrList()
}]);
// 虚拟人管理
app.controller('virtual', ['$scope', 'commonFactory', 'toaster', '$modal', function ($scope, commonFactory, toaster, $modal) {
    $scope.virtualData = {
        pageIndex: 1,
        pageSize: 10,
        userId:''
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.virtualData.pageIndex >= $scope.page) {//判断最大页码
                $scope.virtualData.pageIndex = $scope.page;
                return false;
            }
            $scope.virtualData.pageIndex++
            requestvirtualList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.virtualData.pageIndex <= 1) {//判断最小页码
            $scope.virtualData.pageIndex = 1;
            return false;
        }
        $scope.virtualData.pageIndex--;
        requestvirtualList()
    }
    var requestvirtualList = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/VirtualUsers/GetVirtualUserList", $scope.virtualData, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if (result.Total >= 0) {
                $scope.virtualList = result.List;
                $scope.ListCount = result.Total;
                $scope.page = Math.ceil(result.Total / $scope.virtualData.pageSize);
                for (var i = $scope.virtualList.length - 1; i >= 0; i--) {
                    $scope.virtualList[i].BirthDate = new Date().getFullYear()-new Date($scope.virtualList[i].BirthDate).getFullYear();
                }
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requestvirtualList()
    // 查询
    $scope.queryvirtual = function () {
        $scope.graybtn = true;
        $scope.virtualData.pageIndex = 1;
        requestvirtualList();
    }
     
    // 添加虚拟人
    $scope.addvirtual = function (nid) {
        localStorage.setItem("virtualId", nid)
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                $scope.addvirtualshow = true;
                $scope.newvirtualData = {
                    mobile:'',
                }
                var checkinp = function () {
                    if (!$scope.newvirtualData.mobile) {
                        toaster.pop('warning', '', '请输入要添加的虚拟人手机号');
                        return
                    }
                    return true
                }
                $scope.addgg = true;//防止多次点击
                $scope.ok = function () {
                    if (checkinp() && $scope.addgg) {
                        $scope.addgg = false
                        commonFactory.loading(true)
                        $scope.newvirtualData.mobile = $scope.newvirtualData.mobile.replace(/\n/g,",")
                        commonFactory.ajax("/api/VirtualUsers/SaveBatch", { "":$scope.newvirtualData.mobile }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                $scope.addgg = true;
                                toaster.pop('success', '', '添加虚拟人成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('warning', '', '添加虚拟人失败');
                            }
                        }, function () {
                            commonFactory.loading(false)
                            toaster.pop('error', '', '后台错误');
                        }, "post");
                    }
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
            }],
            size: '',
            backdrop: false
        });
        modalInstance.result.then(function (selectedItem) {
            requestvirtualList()
        }, function () {

        });
    }
    // 编辑虚拟人
    $scope.editvirtual = function (nid) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                $scope.editvirtualshow = true;
                $scope.newvirtualData = {
                    Id:'',
                    TerminalTypeId:''
                }
                var checkinp = function () {
                    if (!$scope.newvirtualData.TerminalTypeId) {
                        toaster.pop('warning', '', '请选择终端类型 ');
                        return
                    }
                    return true
                }
                var initDate = function () {
                    commonFactory.ajax("/api/VirtualUsers/GetVirtualUserById", { "": nid }, function (result) {
                        $scope.newvirtualData.Id = result.Id
                        $scope.newvirtualData.TerminalTypeId = result.TerminalTypeId
                    }, function () {
                        toaster.pop('error', '', '后台错误');
                    }, "post");
                }
                initDate()
                $scope.ok = function () {
                    if (checkinp()) {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/VirtualUsers/UpdateVirtual", $scope.newvirtualData, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '', '编辑虚拟人成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('warning', '', '编辑虚拟人失败');
                                $modalInstance.close();
                            }
                        }, function () {
                            commonFactory.loading(false)
                            toaster.pop('error', '', '后台错误');
                        }, "post");
                    }
                }
                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
            }],
            size: '',
            backdrop: false
        });
        modalInstance.result.then(function (selectedItem) {
            requestvirtualList()
        }, function () {

        });
    }
}]);